package com.elinkway.infinitemovies.adapter;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.elinkway.infinitemovies.utils.LogUtils;

import java.util.ArrayList;

public class MainPagerAdapter extends PagerAdapter {

    private final static String TAG = "MainPagerAdapter";

    private ArrayList<View> mViews;

    public MainPagerAdapter(ArrayList<View> views) {
        mViews = views;
    }

    @Override
    public int getCount() {
        if(mViews != null && mViews.size() == 1){
            return 1;
        }else{
            return Integer.MAX_VALUE;
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        // ((ViewPager) container).removeView(mViews.get(position));
    }

    // 每次滑动的时候生成的组件
    @Override
    public Object instantiateItem(View container, int position) {
        try {
            LogUtils.e(TAG,
                    "!!!!!instantiateItem!!!!!" + position % mViews.size());
            ((ViewPager) container).removeView(mViews.get(position
                    % mViews.size()));
            ((ViewPager) container).addView(
                    mViews.get(position % mViews.size()), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mViews.get(position % mViews.size());
    }

}
